package com.jiangli.service.Search.Impl;

import com.jiangli.entity.Merchant;
import com.jiangli.mapper.MerchantMapper;
import com.jiangli.service.Search.MerchantIndexService;
import com.jiangli.utils.Sreach.RedisKeyUtils;
import com.jiangli.utils.Sreach.SearchUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;



@Service
@Slf4j
public class MerchantIndexServiceImpl implements MerchantIndexService {
    @Autowired
    private GenericIndexService<Merchant, Long> genericIndexService;

    @Autowired
    private MerchantMapper merchantMapper;

    private static final int EXPIRE_HOURS = 12;

    /**
     * 系统启动初始化索引
     */
    @PostConstruct
    @Override
    public void initMerchantIndex() {
        //查询数据库
        log.info("初始化索引");
        List<Merchant> allMerchant = merchantMapper.selectAllMetadata();
        //创建索引
        genericIndexService.batchBuildIndex(
                allMerchant,
                Merchant::getId,
                RedisKeyUtils.MERCHANT_INFO,
                RedisKeyUtils.MERCHANT_INDEX,
                this::getMerchantKeywords,
                EXPIRE_HOURS
        );
    }

    /**
     * 构建单个菜品索引
     */
    @Override
    public void buildMerchantIndex(Merchant merchant) {
        genericIndexService.buildIndex(
                merchant,
                Merchant::getId,
                RedisKeyUtils.MERCHANT_INFO,
                RedisKeyUtils.MERCHANT_INDEX,
                this::getMerchantKeywords,
                EXPIRE_HOURS
        );
    }

    /**
     * 删除菜品索引
     */
    @Override
    public void deleteMerchantIndex(Long dishId) {
        genericIndexService.deleteIndex(dishId, RedisKeyUtils.MERCHANT_INFO);
    }

    /**
     * 获取菜品关键词 TODO 获取菜品哪里关键词
     */
    @Override
    public List<String> getMerchantKeywords(Merchant merchant) {

        List<String> nameKeys = SearchUtils.ikSegment(merchant.getMerchantName());
        log.info("nameKeys: {}", nameKeys);
//        List<String> pinyinKeys = SearchUtils.ikSegment(dish.getPinyin());
//        List<String> tagKeys = SearchUtils.ikSegment(dish.getTags() == null ? "" : dish.getTags());
//        nameKeys.addAll(pinyinKeys);
//        nameKeys.addAll(tagKeys);

        List<String> locationKeys = new ArrayList<>();
        String location = merchant.getLocation();
        if (location != null) {
            locationKeys.addAll(new ArrayList<>(SearchUtils.ikSegment(location)));
        }
// 合并地址关键词到名称关键词列表
        nameKeys.addAll(locationKeys);
        return nameKeys.stream().distinct().collect(Collectors.toList());
    }

    // 委托查询方法
    @Override
    public Set<String> getMerchantIdsByKeyword(String keyword) {
        return genericIndexService.queryEntityIds(RedisKeyUtils.MERCHANT_INDEX, keyword);
    }
//    // 获取实体 TODO 用已有的方法解决
//    public Dish getDishFromCache(Long dishId) {
//
//        return genericIndexService.getEntityFromCache(RedisKeyUtils.DISH_INFO, dishId, Dish.class);
//    }
}
